import { demoOneApi,dmeoTwoApi } from "./api";
import * as echarts from "./echarts.min.js";


//用柱状图展示2020年部分省份所有订单消费额的中位数
//（前10省份，降序排列，若有小数则四舍五入保留两位）
const demoMainOne = async () => {
  let data = (
    await demoOneApi({
      startTime: "2020-01-01 00:00:00",
      endTime: "2020-12-30 00:00:00",
    })
  ).data.data;
  let dataObj = {};
  let datapaixu = [];
  let dataname = [];
  let dataarr = [];
  data.forEach((element) => {
    dataObj[element.provinceName] = [];
  });
  data.forEach((element) => {
    dataObj[element.provinceName].push(element.finalTotalAmount);
  });
  for (const key in dataObj) {
    let datalinsi = dataObj[key].sort((a, b) => {
      return b - a;
    });
    if (datalinsi.length % 2 == 0) {
      let index = datalinsi.length / 2;
      datalinsi = datalinsi[index];
    } else {
      let index = datalinsi.length / 2 + 0.5;
      datalinsi = datalinsi[index];
    }
    dataObj[key] = datalinsi;
  }
  for (const key in dataObj) {
    datapaixu.push({
      name: key,
      data: dataObj[key],
    });
  }
  datapaixu = datapaixu.sort((a, b) => {
    return b.data - a.data;
  });
  datapaixu.slice(0, 10).forEach((e) => {
    dataname.push(e.name);
    dataarr.push(e.data);
  });
  console.log(dataname);
  console.log(dataarr);
  let myEchart = echarts.init(document.getElementById("main"));
  myEchart.setOption({
    name:{
        text:"订单消费额的中位数"
    },
    xAxis:{
        name:"省份",
        data:dataname
    },
    yAxis:{
        name:"消费中位数"
    },
    series:[
        {
            type:"bar",
            data:dataarr
        }
    ]
  })

};

//用基础南丁格尔玫瑰图展示2020年各地区的消费总额占比
const  demoMainTwo = async ()=>{
    let dataarr = [];
    let dataname = [];
    let dataall = [];
    let linshiobj = {};
    let data = (
        await demoOneApi({
          startTime: "2020-01-01 00:00:00",
          endTime: "2020-12-30 00:00:00",
        })
      ).data.data;
      // console.log(data)
      let myEchart = echarts.init(document.getElementById("main"))
      // console.log(myEchart)
      data.forEach(element => {
          linshiobj[element.regionName] = 0;
      });
      data.forEach(element => {
        linshiobj[element.regionName] = linshiobj[element.regionName] + element.finalTotalAmount;
    });
    console.log(linshiobj)
    for (const key in linshiobj) {
        dataall.push({name:key,value:linshiobj[key]})
    }
    dataall = dataall.sort((a,b)=>{
      return b.value - a.value
    })
    dataall.forEach(element => {
      dataname.push(element.name)
      dataarr.push(element.value)
    });
    console.log(dataname,dataarr)
    myEchart.setOption({
      title:{
        text:"2020年各地区的消费总额占比"
      },
      toolbar:{
      },
      legend: {
        top: 'bottom'
      },
      series:[
        {
          type:"pie",
          radius: [50, 140],
          center: ['50%', '50%'],
          data:dataall,
          roseType:'rose',
          itemStyle:{
            borderRadius:8
          },
          label:{
            formatter:'{b} {d}%'
          }
        }
      ]
    })
}

//用折线图展示每年上架商品数量的变化情况
const  demoMainThree = async ()=>{
  let datayeardataarr = [];
  let datayearnamearr = []
  let datayearobj = {};
  let data = (await dmeoTwoApi()).data.data
  data.forEach(element => {
    datayearobj[element.year] = 0;
  });
  data.forEach(element => {
    datayearobj[element.year] = datayearobj[element.year] + 1;
  });
  for (const key in datayearobj) {
      datayearnamearr.push(key)
      datayeardataarr.push(datayearobj[key])
  }
  console.log(datayearnamearr,datayeardataarr)
  let myEchart = echarts.init(document.getElementById("main"))
  myEchart.setOption({
    title:{
      text:"每年上架商品数量的变化情况"
    },
    toorlbar:{},
    xAxis:{
      name:"年份",
      data:datayearnamearr
    },
    yAxis:{
      name:"销售数量"
    },
    series:[
      {
        type:"line",
        data:datayeardataarr
      }
    ]
  })

}


//用条形图展示2020年消费总额最高的10个省份
const demoMainfour = async ()=>{
  let dataobj = {};
  let dataarr = [];
  let dataname = [];
  let datadate = [];
  let data = (
    await demoOneApi({
      startTime: "2020-01-01 00:00:00",
      endTime: "2020-12-30 00:00:00",
    })
  ).data.data;
  data.forEach(element => {
    dataobj[element.provinceName] = 0;
  });
  data.forEach(element => {
    dataobj[element.provinceName] = dataobj[element.provinceName] + element.finalTotalAmount;
  });
  for (const key in dataobj) {
    dataarr.push({name:key,data:dataobj[key]})
  }
  dataarr = dataarr.sort((a,b)=>{
    return b.data - a.data
  })
  dataarr = dataarr.slice(0,10)
  dataarr.forEach(e=>{
    dataname.push(e.name)
    datadate.push(e.data)
  })
  console.log(dataname,datadate)
  let myEchart = echarts.init(document.getElementById("main"))
  myEchart.setOption({
    title:{
      text:"2020年消费总额最高的10个省份"
    },
    toolbar:{},
    xAxis:{
      name:"销售额"
    },
    yAxis:{
      name:"省份",
      data:dataname
    },
    series:[
      {
        type:"bar",
        data:datadate 
      }
    ]
  })
}

/*
用折柱混合图展示2020年各省份平均消费额（四舍五入保留两位小数）
和地区平均消费额（四舍五入保留两位小数）的对比情况，
柱状图展示平均消费额最高的5个省份，
折线图展示这5个省所在的地区的平均消费额变化
*/

const demoMainfive = async ()=>{

}


export const demomain = ()=>{
  demoMainOne()
}
